package com.ynet.middleground.contract.policy;

import java.util.ArrayList;
import java.util.List;

import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
import org.springframework.stereotype.Component;

import com.deepoove.poi.data.CellRenderData;
import com.deepoove.poi.data.RowRenderData;
import com.deepoove.poi.policy.DynamicTableRenderPolicy;
import com.deepoove.poi.policy.MiniTableRenderPolicy;
import com.ynet.middleground.contract.utils.TemplateUtil;

/**
 * 合约表格处理类
 *
 * @author yangbh
 */
@Component
public class DetailTablePolicy extends DynamicTableRenderPolicy {

    /**
     * 填充数据所在行数
     */
    private int startRow = 1;

    @Override
    public void render(XWPFTable table, Object data) {
        if (null == data) {
            return;
        }

        // 获取表头
        XWPFTableRow row0 = table.getRow(0);

        List<List<Object>> params = (List<List<Object>>)data;
        // 删除第一行
        table.removeRow(startRow);

        // 循环插入行
        for (List<Object> rowValue : params) {
            {
                XWPFTableRow insertNewTableRow = table.insertNewTableRow(startRow);

                int cellSize = rowValue.size();

                List<CellRenderData> cellRenderDataList = new ArrayList<>();

                for (int j = 0; j < cellSize; j++) {
                    insertNewTableRow.createCell();
                    String row0CellText = row0.getCell(j).getText();
                    // 数额居右 判断数值单位
                    if (row0CellText.indexOf("元") > -1) {
                        // 生成居右数额
                        cellRenderDataList.add(TemplateUtil.amountCellRenderData(rowValue.get(j)));
                    } else {
                        // 生成居中文本
                        cellRenderDataList.add(TemplateUtil.textCentCellRenderData(rowValue.get(j) + ""));
                    }

                }

                // 合并单元格
                // TableTools.mergeCellsHorizonal(table, startRow, 0, cellSize - 1);
                // 渲染单行人工费数据
                MiniTableRenderPolicy.Helper.renderRow(table, startRow, new RowRenderData(cellRenderDataList));
            }
        }
    }

}
